package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.courierStationDao;
import cn.hs.ph.entity.courierStation;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class courierStationDaoimpl implements courierStationDao {
    @Override
    public int insert(courierStation courierStation) throws SQLException {
        return 0;
    }

    @Override
    public int delete(int courierStationId) {
        return 0;
    }

    @Override
    public int update(courierStation courierStation) {
        return 0;
    }

    @Override
    public List<courierStation> selectAll(Map<String, String> map) throws SQLException {
        String sql = "select * from courierStation where 1=1 ";
        List<courierStation> list = new ArrayList<courierStation>();
        List params = new ArrayList();
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sql+=" and courierStationCommunityName like ? ";
            params.add("%"+communityName+"%");
        }

        String courierStationName = map.get("courierStationName");
        if (courierStationName != null && !courierStationName.equals("")) {
            sql+=" and courierStationName like ? ";
            params.add("%"+courierStationName+"%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !startTime.equals("")) {
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));

        }
        String pa = map.get("pageNum");
        if (pa != null && !pa.equals("")) {
            sql+=" limit ?,? ";
            int pageNum = Integer.parseInt(pa);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] array = params.toArray();
        ResultSet query = JDBCDruidUtil.query(sql, array);
       while (true){
           if (!query.next())break;
           courierStation courierStation = new courierStation(
                   query.getInt(1),
                   query.getDate(2),
                   query.getString(3),
                   query.getString(4),
                   query.getInt(5),
                   query.getString(6),
                   query.getString(7),
                   query.getString(8),
                   query.getString(9),
                   query.getInt(10),
                   query.getString(11),
                   query.getInt(12),
                   query.getInt(13)
           );
           list.add(courierStation);



       }

JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) throws SQLException {
        int conut=0;
        String sql = "select count(1) from courierStation where 1=1 ";

        List params = new ArrayList();
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sql+=" and courierStationCommunityName like ? ";
            params.add("%"+communityName+"%");
        }

        String courierStationName = map.get("courierStationName");
        if (courierStationName != null && !courierStationName.equals("")) {
            sql+=" and courierStationName like ? ";
            params.add("%"+courierStationName+"%");
        }
        String startTime = map.get("startTime");
        if (startTime != null && !startTime.equals("")) {
            sql+=" and addTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));

        }

        Object[] array = params.toArray();
        ResultSet query = JDBCDruidUtil.query(sql, array);
        while (true){
            if (!query.next())break;
            conut=query.getInt(1);
        }


        JDBCDruidUtil.close();
        return conut;
    }
}
